package com.shijunhao.graduation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.DiscussDto;
import com.shijunhao.graduation.domain.vo.DiscussVo;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessForumDiscussService;
import com.shijunhao.graduation.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/business-forum-discuss")
@Api(tags = "论坛评论")
public class BusinessForumDiscussController {

    @Resource
    private IBusinessForumDiscussService businessForumDiscussService;

    @GetMapping("info/{id}")
    @ApiOperation("评论列表")
    public ResultCode<IPage<DiscussVo>> info(@ApiParam(value = "论坛id", example = "1") @PathVariable Long id) {
        return ResultCode.success(businessForumDiscussService.page(PageUtils.getPage(), id));
    }

    @PostMapping("insert")
    @ApiOperation("新增")
    @OperationLog(target = OperationTarget.FORUM_DISCUSS,type = OperationType.INSERT,description = "新增评论")
    public ResultCode<Long> insert(@RequestBody DiscussDto dto) {
        Long insert = businessForumDiscussService.insert(dto);
        if (insert != null)
            return ResultCode.success("插入成功", insert);
        else
            return ResultCode.error("插入失败");
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除")
    @OperationLog(target = OperationTarget.FORUM_DISCUSS,type = OperationType.DELETE,description = "删除评论")
    public ResultCode<Boolean> delete(@PathVariable Long id) {
        boolean delete = businessForumDiscussService.delete(id);
        if (delete)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }
}
